import { isWatcher } from "./watcher"
let uid = 0
export default class Dep {
  constructor() {
    console.log('Dep constructor');
    this.id = uid++
    this.deps = []
  }

  addDep(watcher) {
    this.deps.push(watcher)
  }

  notify() {
    this.deps.forEach(watcher => watcher.update())
  }
  depend() {
    if (Dep.target) {
      // this.addDep(Dep.target);
      Dep.target.addDep(this)
    }
  }
}

Dep.target = null
const targetStack = []

export function pushTarget(target) {
  if (!isWatcher(target)) return;
  targetStack.push(target)
  Dep.target = target
}

export function popTarget() {
  targetStack.pop()
  Dep.target = targetStack[targetStack.length - 1]
}